package edu.mis.modules.roomType;

import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import edu.mis.model.RoomType;
import edu.mis.modules.role.RoleInterceptor;

/**
 * 房间类型控制器
 *
 */
public class RoomTypeController extends Controller {
	
	public static RoomTypeService service = new RoomTypeService();
	@Clear(RoleInterceptor.class)
	public void index() {
		render("roomType.html");
	}
	@Clear(RoleInterceptor.class)
	public void list(){
		setAttr("data", service.findAllRoomType());
		renderJson();  
	}
	

	/**
	 * 添加日志
	 */
	public void save(){
		RoomType roomType  =  getModel(RoomType.class); //注意：使用getModel方法获取表单值非常方便，不需要对每个字段进行设置，直接得到model。注意使用此方法前端表单name必须名称为“对象名.属性名”
		if(service.saveType(roomType)){
			setAttr("result", true); //前端ajax回调值，进行交互判断  if(data.result){.......} 注意两个result对应
			setAttr("msg", "添加房间类型成功!"); //前端ajax回调值，交互消息框layer.msg(data.msg)
		}else{
			setAttr("result",false);
			setAttr("msg", "添加房间类型失败，该房间类型已有!"); 
		}
		renderJson();  //返回json，用于向前台页面返回结果
	}
	
	public void edit() {
		setAttr("roomType", service.findRoomTypeById(getParaToInt("id")));  //getPara获取参数为string类型，getParaToInt将获得的String转为int
		renderJson();
	}
	
	/**
	 * 修改日志
	 */
	public void update(){
		RoomType roomType  =  getModel(RoomType.class);
		if(service.updateType(roomType)){
			setAttr("result", true);
			setAttr("msg", "修改房间类型成功!");
		}else{
			setAttr("result", false);
			setAttr("msg", "修改失败，没有变动!");
		}
		renderJson();
	}
	
	/**
	 * 删除日志
	 * 测试访问地址：localhost:8080/roomType/delete?id=1
	 */
	public void delete() {
		if(service.deleteRoomTypeById(getParaToInt("id"))){
			setAttr("result", true);
			setAttr("msg", "删除房间类型成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
	@Clear(RoleInterceptor.class)
	public void view_addToCart() {
		setAttr("roomType", service.findRoomTypeById(getParaToInt("id")));  //此处commodity应与表单中${(commodity.name)!}等中的commodity保持一致。
		render("addRoomTypeToCart.html");
	}

}


